package com.example.zhinongbao.activity;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import com.example.zhinongbao.R;
import com.example.zhinongbao.model.Address;

public class AddAddressActivity extends AppCompatActivity {

    public static final String EXTRA_ADDRESS = "extra_address";

    private EditText etName, etPhone, etRegion, etDetail;
    private Switch switchDefault;
    private Button btnSave;
    private Address editAddress;

    public static void start(Context context) {
        start(context, null);
    }

    public static void start(Context context, Address address) {
        Intent intent = new Intent(context, AddAddressActivity.class);
        if (address != null) {
            intent.putExtra(EXTRA_ADDRESS, address);
        }
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);

        // 获取Intent数据
        editAddress = getIntent().getParcelableExtra(EXTRA_ADDRESS);

        // 初始化视图
        etName = findViewById(R.id.et_name);
        etPhone = findViewById(R.id.et_phone);
        etRegion = findViewById(R.id.et_region);
        etDetail = findViewById(R.id.et_detail);
        switchDefault = findViewById(R.id.switch_default);
        btnSave = findViewById(R.id.btn_save);

        // 设置返回按钮
        findViewById(R.id.btn_back).setOnClickListener(v -> finish());

        // 如果是编辑模式，填充数据
        if (editAddress != null) {
            etName.setText(editAddress.getName());
            etPhone.setText(editAddress.getPhone());
            etRegion.setText(editAddress.getRegion());
            etDetail.setText(editAddress.getDetail());
            switchDefault.setChecked(editAddress.isDefault());
        }

        // 保存按钮点击事件
        btnSave.setOnClickListener(v -> saveAddress());
    }

    private void saveAddress() {
        // 获取输入值
        String name = etName.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();
        String region = etRegion.getText().toString().trim();
        String detail = etDetail.getText().toString().trim();
        boolean isDefault = switchDefault.isChecked();

        // 验证输入
        if (name.isEmpty() || phone.isEmpty() || region.isEmpty() || detail.isEmpty()) {
            // 显示错误提示
            return;
        }

        // 创建/更新地址对象
        Address address;
        if (editAddress != null) {
            // 更新模式
            address = editAddress;
            address.setName(name);
            address.setPhone(phone);
            address.setRegion(region);
            address.setDetail(detail);
            address.setDefault(isDefault);
        } else {
            // 新增模式
            address = new Address(0, name, phone, region, detail, isDefault);
        }

        // TODO: 保存地址到数据库或服务器

        // 返回结果
        Intent resultIntent = new Intent();
        resultIntent.putExtra(EXTRA_ADDRESS, address);
        setResult(RESULT_OK, resultIntent);
        finish();
    }
}